package com.neusoft.neuiotms.dcam.fixed.repository.spareConsume.impl;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.fixed.model.spareconsume.QSpareConsumeBack;
import com.neusoft.neuiotms.dcam.fixed.model.spareconsume.SpareConsumeBack;
import com.neusoft.neuiotms.dcam.fixed.repository.spareConsume.CustomizedSpareConsumeBackRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public class CustomizedSpareConsumeBackRepositoryImpl extends AbstractRepository
        implements CustomizedSpareConsumeBackRepository {
    @Override
    protected Class<?> getModelClass() {
        return SpareConsumeBack.class;
    }

    @Override
    public Page<SpareConsumeBack> search(Pageable pageable, Searchable searchable) {
        final QSpareConsumeBack qSpareConsumeBack = QSpareConsumeBack.spareConsumeBack;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<SpareConsumeBack> query = this.query().selectFrom(qSpareConsumeBack);
        query.where(where).orderBy(qSpareConsumeBack.updateTime.desc(),qSpareConsumeBack.createTime.desc());
        return super.search(query,pageable);
    }

    private BooleanBuilder buildWhere(Searchable searchable) {
        final BooleanBuilder where = new BooleanBuilder();
        final QSpareConsumeBack qSpareConsumeBack = QSpareConsumeBack.spareConsumeBack;
        if (StringUtils.isNotEmpty(searchable.getStrValue("workOrderNum"))) {
            where.and(super.equalsStr(qSpareConsumeBack.workNum, searchable, "workOrderNum"));//备品类型
        }
        if(StringUtils.isNotEmpty(searchable.getStrValue("taskId"))){
            where.and(super.equalsStr(qSpareConsumeBack.taskId,searchable,"taskId"));
        }
        return where;
    }
}
